<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Index extends Controller
{
    public function _initialize()
    {
        $config=Db::table('config')->where('id',1)->find();
        $qq=$config['qq'];
        $phone=$config['phone'];
        $email=$config['email'];
        $address=$config['address'];
        $this->assign('qq', $qq);
        $this->assign('phone', $phone);
        $this->assign('email', $email);
        $this->assign('address', $address);


        $links=Db::table('link')->order('sort desc,id desc')->select();
        $this->assign('links', $links);

    }

    public function apply() {
        $req_data= $this->request->post();
        if ($req_data) {
            $req_data['apply_time'] = time();
            $ids = Db::table('apply')->where(['tel' => $req_data['tel'], 'company' => $req_data['company']])->find();
            if ($ids) {
                $result = Db::table('apply')->where('id',$ids['id'])->setField($req_data);
            } else {
                $result = Db::table('apply')->insertGetId($req_data);
            }

            if (false === $result) {
                return json(['code' =>1,'count' =>0,'msg' =>'提交失败','data'=>'']);
            } else {
                $to = '517599331@qq.com';
                $name = '“浙江四为网络”-'.$req_data['tel'].'留言提醒';  //标题
                $body = '公司名称：'.$req_data['company'].',电话：'.$req_data['tel'].',<br>留言内容：'.$req_data['remark'];

                if($to){
                    $result =think_send_mail($to,$name,$name,$body); //直接调用发送即可
                    if($result){
                        return json(['code' =>0,'msg' =>'提交成功~客服将在48小时候内联系您~','data'=>'','res'=>$result]);
                    }else{
                        return json(['code' =>1,'msg' =>'提交失败','to'=>$to,'name'=>$name,'body'=>$body,'res'=>$result]);
                    }
                }
            }
        }
    }


    public function index()
    {
        $cate_id=1;
        $this->assign('cate_id', $cate_id);
        $seo=Db::table('category')->where('id',$cate_id)->find();
        $this->assign('title', $seo['seo_title']);
        $this->assign('keywords', $seo['seo_keywords']);
        $this->assign('description', $seo['seo_description']);

        $news1=Db::table('article')->where('cid',23)->order('sort desc,id')->limit(5)->select();
        $this->assign('news1',$news1);
        $news2=Db::table('article')->where('cid',24)->order('sort desc,id')->limit(5)->select();
        $this->assign('news2',$news2);
        $news3=Db::table('article')->where('cid',25)->order('sort desc,id')->limit(5)->select();
        $this->assign('news3',$news3);
        return $this->fetch();
    }
    public function solution(){
        $cate_id=33;
        $this->assign('cate_id', $cate_id);
        $seo=Db::table('category')->where('id',$cate_id)->find();
        $this->assign('title', $seo['seo_title']);
        $this->assign('keywords', $seo['seo_keywords']);
        $this->assign('description', $seo['seo_description']);
        return $this->fetch();
    }
    public function about(){
        $cate_id=7;
        $this->assign('cate_id', $cate_id);
        $seo=Db::table('category')->where('id',$cate_id)->find();
        $this->assign('title', $seo['seo_title']);
        $this->assign('keywords', $seo['seo_keywords']);
        $this->assign('description', $seo['seo_description']);
        return $this->fetch();
    }
    public function pro(){
        $cate_id=32;
        $this->assign('cate_id', $cate_id);
        $seo=Db::table('category')->where('id',$cate_id)->find();
        $this->assign('title', $seo['seo_title']);
        $this->assign('keywords', $seo['seo_keywords']);
        $this->assign('description', $seo['seo_description']);
        return $this->fetch();
    }

    public function seo(){
        $cate_id=34;
        $this->assign('cate_id', $cate_id);
        $seo=Db::table('category')->where('id',$cate_id)->find();
        $this->assign('title', $seo['seo_title']);
        $this->assign('keywords', $seo['seo_keywords']);
        $this->assign('description', $seo['seo_description']);
        return $this->fetch();
    }
    public function contact(){
        $cate_id=18;
        $this->assign('cate_id', $cate_id);
        $seo=Db::table('category')->where('id',$cate_id)->find();
        $this->assign('title', $seo['seo_title']);
        $this->assign('keywords', $seo['seo_keywords']);
        $this->assign('description', $seo['seo_description']);

        $this->assign('content', $seo['content']);


        return $this->fetch();
    }
    public function news(){
        $cate_id=17;
        $this->assign('cate_id', $cate_id);


        $cid = input('cid');
        $where=[];

        if($cid){
            $seo=Db::table('category')->where('id',$cid)->find();
            $where['b.id']=$cid;
            $this->assign('cid', $cid);
        }else{
            $seo=Db::table('category')->where('id',$cate_id)->find();
            $where['b.pid']=$cate_id;
            $this->assign('cid', 0);
        }


        $this->assign('title', $seo['seo_title']);
        $this->assign('keywords', $seo['seo_keywords']);
        $this->assign('description', $seo['seo_description']);


        $category_list = Db::table('category')->where('pid',17)->order('id')->select();
        $this->assign('category_list', $category_list);


        $list = Db::table('article a')->field('a.*,b.title category_name')->join('category b','b.id= a.cid')->where($where)->order('a.sort desc,a.created_at desc')->paginate(8);
        $this->assign('list', $list);

        return $this->fetch();

    }

    public function info(){
        $id = input('id');
        if($id){
            $article=Db::table('article a')->field('a.*,b.title category_name,b.pid')->join('category b','b.id= a.cid')->where('a.id',$id)->find();
            $this->assign('article',$article);

            if($article['pid']>0){
                $cate_id=$article['pid'];
            }else{
                $cate_id=$article['cid'];
            }
            $this->assign('cate_id', $cate_id);

            $seo=Db::table('category')->where('id',$cate_id)->find();
            $this->assign('title', $seo['seo_title']);
            $this->assign('keywords', $seo['seo_keywords']);
            $this->assign('description', $seo['seo_description']);
        }
        return $this->fetch();
    }


    public function check_user()
    {
        $data = $this->request->post();
        if($data){
            $res=Db::table('user')->where(['username' => $data['username'], 'password' => md5($data['password'])])->find();
            if ($res) {
                Session::set('admin.id',$res['id']);
                Session::set('admin.name',$res['username']);

                if(Session::get('admin')){
                    return json(['msg' =>'登录成功', 'code' =>0]);
                }else{
                    return json(['msg' =>'登录失败', 'code' =>1]);
                }
            }else{
                return json(['msg' =>'用户名或者密码错误!', 'code' =>1]);
            }
        }
    }

}
